package com.ruoyi.market.service.warning;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.market.model.param.WarnInfoParam;
import com.ruoyi.market.model.result.WarnInfoResult;
import com.ruoyi.market.service.IWarningService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class DaHanSmsWarningService implements IWarningService {

    @Override
    public WarnInfoResult handleWarnInfo(WarnInfoParam warnInfoParam) {

        WarnInfoResult warnInfoResult = new WarnInfoResult();
        warnInfoResult.setTitle("大汉短信余额");
        try {

            String requestUrl = "http://www.dh3t.com/json/sms/Balance";
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("account", "dh47239");
            paramMap.put("password", "a814fc3270e904f33f7eef2df7975ef4");

            HttpRequest httpRequest = HttpUtil.createPost(requestUrl);
            String response = httpRequest.body(JSON.toJSONString(paramMap)).execute().body();

            JSONObject result = JSON.parseObject(response);
            String resultCode = result.getString("result");
            String desc = result.getString("desc");

            if (StrUtil.equals(String.valueOf(resultCode), "0")){
                JSONObject smsBalance = result.getJSONObject("smsBalance");
                String amount = smsBalance.getString("amount");
                warnInfoResult.setMessage(amount);
                return warnInfoResult;
            }
            warnInfoResult.setMessage(resultCode + "|" + desc);

        }catch (Exception exception){
            exception.printStackTrace();
            warnInfoResult.setMessage(exception.getMessage());
        }
        return warnInfoResult;
    }
}
